package javaBase.chapter02.cse;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class case2_6 {
    private Map<String, String> users = new HashMap<>();

    public void login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        if (users.containsKey(username) && users.get(username).equals(password)) {
            System.out.println("登录成功！");
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    public void register() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入密码：");
        String password = scanner.nextLine();

        if (users.containsKey(username)) {
            System.out.println("用户名已存在！");
        } else {
            users.put(username, password);
            System.out.println("注册成功！");
        }
    }

    public void viewUsers() {
        System.out.println("所有用户：");
        for (Map.Entry<String, String> entry : users.entrySet()) {
            System.out.println("用户名：" + entry.getKey() + "，密码：" + entry.getValue());
        }
    }

    public void exit() {
        System.out.println("退出系统！");
        System.exit(0);
    }

    public static void main(String[] args) {
        case2_6 userManager = new case2_6();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("请选择功能：1.登录 2.注册 3.查看用户 4.退出");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    userManager.login();
                    break;
                case 2:
                    userManager.register();
                    break;
                case 3:
                    userManager.viewUsers();
                    break;
                case 4:
                    userManager.exit();
                    break;
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }
}

